#*-*coding:utf8*-*#
__author__ = 'guo'
from django.shortcuts import render,get_object_or_404
from .models import *

# Create your views here.
from libs.Base import *
from django.http import Http404
from django.contrib.auth import decorators
from app.salers.models import *

from django.core.paginator import Paginator
from django.core.paginator import PageNotAnInteger
from django.core.paginator import EmptyPage
import string
from django.views.decorators.csrf import csrf_exempt
from django.contrib.auth import decorators

# 主页
# @decorators.login_required(login_url="/crm/login/")

# 客户列表
@decorators.login_required(login_url="/crm/login/")
@csrf_exempt
def index(request):
    customerList = Customer.objects.filter(saler=request.user.id)
    totalCount = len(customerList)
    #每页显示多少条
    numPerPage = request.POST.get("numPerPage") if request.POST.get("numPerPage") else 20
    paginator = Paginator(customerList, numPerPage)
    #当前页码
    pageNum = request.POST.get("pageNum") if request.POST.get("pageNum") != None else 1
    list = paginator.page(pageNum)
    return render(request, 'customer/customer.html', locals())


#添加客户
# @csrf_exempt
@decorators.login_required(login_url="/crm/login/")
def addcustomer(request):
    if request.method == "POST":
        CusObj = Customer.objects.create(
            customername = request.POST.get("customername"),
            company = request.POST.get("companyname"),
            gender = request.POST.get("gender"),
            age = request.POST.get("age"),
            mobile = request.POST.get("mobile"),
            qq = request.POST.get("qq"),
            email = request.POST.get("email"),
            wangwang = request.POST.get("wangwang"),
            birthday = request.POST.get("birthday"),
            partment = request.POST.get("partment"),
            custype = request.POST.get("custype"),
            custatus = request.POST.get("custatus"),
            description = request.POST.get("description"),
            saler = get_object_or_404(User, pk=request.user.id),
            industry = Industry.objects.get(pk=request.POST.get("industry")),
            level = Level.objects.get(pk=request.POST.get("level")),
        )
        if CusObj:
            return OutJson(200,"添加成功")
        else:
            return OutJson(300,"添加失败")
    else:
        customergroup = CustomerGroup.objects.filter(ofsaleid_id = request.user.id)
        industrylist = Industry.objects.all()
        levellist = Level.objects.all()
        return render(request, 'customer/addcustomer.html', locals())



#编辑客户
@decorators.login_required(login_url="/crm/login/")
def editcustomer(request):
    if request.method == "POST":
        cid = request.POST.get("id")
        CusObjUpdate = Customer.objects.filter(id=cid).update(
            customername = request.POST.get("customername"),
            company = request.POST.get("companyname"),
            gender = request.POST.get("gender"),
            age = request.POST.get("age"),
            mobile = request.POST.get("mobile"),
            qq = request.POST.get("qq"),
            email = request.POST.get("email"),
            wangwang = request.POST.get("wangwang"),
            birthday = request.POST.get("birthday"),
            partment = request.POST.get("partment"),
            custype = request.POST.get("custype"),
            custatus = request.POST.get("custatus"),
            description = request.POST.get("description"),
            saler = get_object_or_404(User, pk=request.user.id),
            industry = Industry.objects.get(pk=request.POST.get("industry")),
            level = Level.objects.get(pk=request.POST.get("level")),
        )
        if CusObjUpdate:
            return OutJson(200,"更新成功")
        else:
            return OutJson(300,"更新失败")
    else:
        cid = request.GET.get("id")
        CustomerObj = Customer.objects.filter(id=cid)[0]
        customergroup = CustomerGroup.objects.filter(ofsaleid_id = request.user.id)
        industrylist = Industry.objects.all()
        levellist = Level.objects.all()
        return render(request, 'customer/editcustomer.html', locals())


#添加组别：如果是boss或者管理员添加的话 用别的逻辑处理
#如果是销售人员自己添加的话，组表里面的ofsalerid 字段就填写自己的id
@decorators.login_required(login_url="/crm/login/")
def addgroup(request):
    if request.method == "POST":
        #boss或者管理人员添加
        #售售人员自己添加
        gname = request.POST.get('groupname')
        user = get_object_or_404(User, pk=request.user.id)
        if CustomerGroup.objects.create(groupname=gname,ofsaleid=user):
            return OutJson(200, "添加分组成功")
        else:
            return OutJson(300, "添加分组失败")
    else:
        return render(request, 'customer/addgroup.html', locals())